package com.minjiang.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.minjiang.common.result.Result;
import com.minjiang.model.system.MyComment;
import com.minjiang.model.system.MyDynamic;
import com.minjiang.model.system.MyUser;
import com.minjiang.model.vo.MyCommentQueryVo;
import com.minjiang.model.vo.MyCommentVo;
import com.minjiang.model.vo.MyDynamicVo;
import com.minjiang.system.service.sec.MyCommentService;
import com.minjiang.system.service.sec.MyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "评论管理")
@RestController
@RequestMapping("/admin/system/myComment")
public class MyCommentController {
    @Autowired
    private MyCommentService myCommentService;
    @Autowired
    private MyUserService myUserService;

    @ApiOperation(value = "获取评论分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "myCommentVo", value = "查询对象", required = false)
                    MyCommentQueryVo myCommentQueryVo) {
        Page<MyComment> pageParam = new Page<>(page, limit);
        IPage<MyComment> pageModel = myCommentService.selectPage(pageParam, myCommentQueryVo);
        List<MyCommentVo> myCommentVos=new ArrayList<>();
        for (MyComment record : pageModel.getRecords()) {
            MyCommentVo myCommentVo = new MyCommentVo();
            BeanUtils.copyProperties(record,myCommentVo);
            MyUser myUser = myUserService.getById(record.getUserId());
            myCommentVo.setUserName(myUser.getNickname());
            myCommentVos.add(myCommentVo);
        }
        Page<MyCommentVo> myCommentVoPage = new Page<MyCommentVo>();
        myCommentVoPage.setTotal(pageModel.getTotal());
        myCommentVoPage.setCurrent(pageModel.getCurrent());
        myCommentVoPage.setRecords(myCommentVos);
        myCommentVoPage.setSize(pageModel.getSize());
        myCommentVoPage.setPages(pageModel.getPages());
        return Result.ok(myCommentVoPage);
    }

    @ApiOperation(value = "根据id获取评论")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable String id) {
        MyComment comment = myCommentService.getById(id);
        MyCommentVo myCommentVo = new MyCommentVo();
        BeanUtils.copyProperties(comment,myCommentVo);
        MyUser byId = myUserService.getById(comment.getUserId());
        myCommentVo.setUserName(byId.getNickname());
        return Result.ok(myCommentVo);
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable String id){
        boolean isOk = myCommentService.removeById(id);
        if (isOk){
            return Result.ok();
        }
        return Result.fail("删除评论失败");
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("/save")
    public Result save(@RequestBody MyComment comment){
        boolean save = myCommentService.save(comment);
        if (save){
            return Result.ok();
        }
        return Result.fail("添加评论失败");
    }

    @ApiOperation(value = "更新评论")
    @PutMapping("/update")
    public Result updateById(@RequestBody MyComment comment) {
        myCommentService.updateById(comment);
        return Result.ok();
    }
}
